package com.dnake.libface.core;

import android.text.TextUtils;
import android.util.Log;

import com.dnake.libface.Face;

public class EventAnalyzer {

    private static DnakeEventListener mListener;

    public static void event(String url, String body) {
        Log.e("cxy","url = "+url+"  body= "+body);
        switch (url){
            case "/ui/run":
                Dmsg.ack(200, null);
                break;

            case "/ui/v170/card":
                delCard(body);
                break;
            case "/ui/v170/face/register":
                delFaceRegister(body);
                break;
            case "/ui/v170/face/ident":
                delFaceIdent(body);
                break;
            case "/ui/face/reboot":
                delFaceReboot(body);
                break;
            case "/ui/face/capture":
                delFaceCapture(body);
                break;
            case "/ui/face/lived":
                delFaceLived(body);
                break;
            case "/ui/ir/detect":
                delIrDetect(body);
                break;


        }
    }

    /**
     *人体识别
     */
    private static void delFaceIdent(String body) {
        Dmsg.ack(200, null);

        Dxml p = new Dxml();
        p.parse(body);
        int d = p.getInt("/params/data", 0);
        int id = p.getInt("/params/id", 0);
        int uid = p.getInt("/params/uid", 0);
        int black = p.getInt("/params/black", 0);

        String url = p.getText("/params/url");

        if (mListener != null) {
            mListener.onFaceIdent(d,id,uid,black);
        }
    }

    /**
     * 人脸后台启动
     */
    private static void delFaceReboot(String body) {
        Dmsg.ack(200, null);
        if (mListener != null) {
            mListener.onFaceReboot();
        }
    }

    /**
     * 人脸抓拍
     */
    private static void delFaceCapture(String body) {
        Dmsg.ack(200, null);
        Dxml p = new Dxml();
        p.parse(body);
        String url = p.getText("/params/url");
        if (mListener != null) {
            mListener.onFaceCapture(url);
        }
    }

    /**
     * 活体检测
     */
    private static void delFaceLived(String body) {
        Dmsg.ack(200, null);
        Dxml p = new Dxml();
        p.parse(body);
        int mode = p.getInt("/params/mode", 0);
        String url = p.getText("/params/url");
        if (mListener != null) {
            mListener.onFaceLived(mode,url);
        }
    }

    /**
     * 人体感应
     */
    private static void delIrDetect(String body) {
        Dmsg.ack(200, null);
        if (TextUtils.isEmpty(body)) {
            return;
        }

        if(mListener != null){
            mListener.onFaceDetect();
        }

    }

    /**
     * 人脸注册
     * @param body
     */
    private static void delFaceRegister(String body) {
        Dmsg.ack(200, null);
        if(mListener != null){
            mListener.onFaceRegister();
        }

    }


    private static void delCard(String body) {
        Dmsg.ack(200, null);
        Dxml p = new Dxml();
        p.parse(body);

        Log.e("====+++++>>","card"+body);

        String card = p.getText("/params/data", "");
        if (card.isEmpty())
            card = p.getText("/params/datab", "");
        int auth = p.getInt("/params/auth", -1);
        if(mListener != null){
            mListener.onCardResult(card,auth);
        }
    }

    /**
     * 设置事件监听回调
     * @param listener 事件回调，包括人脸识别，注册，人体感应，卡号读取等
     */
    public static void setEventListener(DnakeEventListener listener){
        mListener = listener;
    }
}
